package com.todo.biz.conf;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangwenxiang
 * @date 2024/3/5 23:22
 * @description 单机模式配置
 */
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.password}")
    private String redisPassword;

    @Value("${redisson.singleServerConfig.connectionMinimumIdleSize}")
    private Integer connectionMinimumIdleSize;

    @Value("${redisson.singleServerConfig.connectionPoolSize}")
    private Integer connectionPoolSize;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        // 配置单节点模式
        config.useSingleServer()
                .setAddress("redis://" + redisHost + ":" + redisPort) // 设置 Redis 地址
                .setPassword(redisPassword)
                .setConnectionMinimumIdleSize(connectionMinimumIdleSize)
                .setConnectionPoolSize(connectionPoolSize); // 设置 Redis 密码

        // 创建 Redisson 客户端
        return Redisson.create(config);
    }
}
